<?php	
	//die('script now work');
	
	error_reporting( 0 );
	
	require_once('config.php');
	include_once('function.php');			
		

	// Create connection
	$con = connectDB($host,$user,$pass,$db);			
	
	// process import
	$products = array();

	// Estimated time to process import
	$time_eta = 0;	

	if(!empty($_POST) && isset($_FILES["file"])) // 
	{		
		// Start time
		$time_start = microtime(true);

		if ( isset($_FILES["file"])) 
		{
			//if there was an error uploading the file
			if ($_FILES["file"]["error"] > 0) 
			{
				echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
				die();
			}

			// move uploaded file
			move_uploaded_file($_FILES["file"]["tmp_name"],  "upload/products.xls");

			set_include_path(get_include_path() . PATH_SEPARATOR . 'includes/Classes/');
			include 'PHPExcel/IOFactory.php';

			// This is the file path to be uploaded.
			$inputFileName = "upload/products.xls"; 

			try {
				$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
			} catch(Exception $e) {
				die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
			}


			$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
			$arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet		
			
			// Remove first array - heading title. 
			array_splice($allDataInSheet, 0, 1);			
			
			foreach($allDataInSheet as $data)
			{
				$code 	   		= $data['A'];
				$xs_code 		= $data['B'];
				$xs 			= $data['C'];
				$s_code  		= $data['D'];
				$s  			= $data['E'];
				$m_code 		= $data['F'];
				$m   			= $data['G'];
				$l_code   		= $data['H'];
				$l   			= $data['I'];
				$xl_code    		= $data['J'];
				$xl    			= $data['K'];
				$xxl_code    		= $data['L'];
				$xxl    			= $data['M'];
				$xxxl_code    		= $data['N'];
				$xxxl    			= $data['O'];						
				
				// get product id
				$sql = "SELECT * FROM products WHERE code LIKE '$code' LIMIT 1";
				
				$result = mysqli_query($con, $sql);
				$product_row = mysqli_fetch_row($result);
				$product_id = $product_row[0];
								
				if($product_id > 0)
				{								
				    $sql = "UPDATE `product_sizes` SET `quantity` = CASE size ";
					
					if(!empty($xxxl))
					{
						$sql .= " WHEN 'xxxl' THEN $xxxl  ";
					}
					if(!empty($xxl))
					{
						$sql .= " WHEN 'xxl' THEN $xxl  ";
					}
					if(!empty($xs))
					{
						$sql .= " WHEN 'xs' THEN $xs  ";
					}
					if(!empty($xl))
					{
						$sql .= " WHEN 'xl' THEN $xl  ";
					}		
					if(!empty($s))
					{
						$sql .= " WHEN 's' THEN $s  ";
					}		
					if(!empty($m))
					{
						$sql .= " WHEN 'm' THEN $m ";
					}			
					if(!empty($l))
					{
						$sql .= " WHEN 'l' THEN $l ";
					}			
					$sql .= " ELSE `quantity` END WHERE `product_id` = $product_id "; 		
												
					mysqli_query($con, $sql);						
					
					// update lần 2
					$sql = "UPDATE `product_sizes` SET `code` = CASE size ";
					
					if(!empty($xxxl_code))
					{
						$sql .= " WHEN 'xxxl' THEN '$xxxl_code'  ";
					}
					if(!empty($xxl_code))
					{
						$sql .= " WHEN 'xxl' THEN '$xxl_code'  ";
					}
					if(!empty($xs_code))
					{
						$sql .= " WHEN 'xs' THEN '$xs_code'  ";
					}
					if(!empty($xl_code))
					{
						$sql .= " WHEN 'xl' THEN '$xl_code'  ";
					}		
					if(!empty($s_code))
					{
						$sql .= " WHEN 's' THEN '$s_code'  ";
					}		
					if(!empty($m_code))
					{
						$sql .= " WHEN 'm' THEN '$m_code' ";
					}			
					if(!empty($l_code))
					{
						$sql .= " WHEN 'l' THEN '$l_code' ";
					}			
					$sql .= " ELSE `code` END WHERE `product_id` = $product_id "; 
					
					mysqli_query($con, $sql);	
				}
			}

			echo "<script>alert('Sản phẩm nhập thành công !')</script>";
		}

		// End time
		$time_end = microtime(true);		
		$time_eta = ($time_end - $time_start)/60;
	}	
		
	
	/*
	echo '<pre>';
	print_r($allDataInSheet);
	echo '</pre>';
	*/	
	
	// import link, export link, setting link
	$main_site = "http://shop.aothun.vn/export/aothunvn/";
	$import_link = "http://shop.aothun.vn/export/aothunvn/importw/";
	$export_link = "http://shop.aothun.vn/export/aothunvn/exportw/";
	$setting_link = "http://shop.aothun.vn/export/aothunvn/importw";	
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8"/>
		 <meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Import dữ liệu</title>
		
		<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
		<!-- Bootstrap theme -->
		<link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">		
		<link href="css/theme.css" rel="stylesheet">		
		<script>				
			var local_site = "http://localhost/";				
			
			function submitForm()
			{				
				document.getElementById("my_form").submit();
			}
		</script>
	</head>
	<body role="document">
		<?php require_once 'banner.php' ?>
				
		<div class="container theme-showcase" role="main">
			 <div class="page-header">
				<h1>Nhập sản phẩm</h1>
			  </div>

			  <?php if($time_eta > 0): ?>
			  <div class="alert alert-success">
		        Quy trình thực hiện <?=$time_eta ?> phút.
		      </div>
		  	  <?php endif; ?>

			  <p>								
				<button type="button" class="btn btn-lg btn-success" onClick="submitForm()">Import dữ liệu</button>				
			  </p>
			  
			  <!-- Input file .xls to update data -->
			  <form role="form" method="post" enctype="multipart/form-data" id="my_form" action="<?php echo $_SERVER['PHP_SELF'];?>">
				  <div class="form-group">
					<label for="exampleInputFile">Input xls</label>
					<input type="file" id="exampleInputFile" name="file">
					<input type="hidden" name="to_make_it_works">
					<p class="help-block">* Only .xls file is available</p>
				  </div>				  
			  </form>
		</div>
	</body>
</html>